//
//  AdMoreDrawAdController.m
//  AdMoreSDKDemo
//
//  Created by Hayder on 2023/12/7.
//

#import "AdMoreDrawAdController.h"

@interface AdMoreDrawAdController ()<AdMoreDrawAdManagerDelegate,AdMoreDrawAdDelegate>

@property (nonatomic, strong) AdMoreDrawAdManager *adManager;

//信息流对象数组
@property (nonatomic, strong) NSMutableArray *drawAdModels;

/**示例：加载1个**/
@property (nonatomic, strong) AdMoreDrawAd *drawAd;

@property (nonatomic, strong) UIScrollView *contentView;

@property (nonatomic, strong) UIView *lastShowView;

@end

@implementation AdMoreDrawAdController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupBtn];
    
    [self.view addSubview:self.contentView];
    self.drawAdModels = [NSMutableArray array];
}

- (void)setupBtn
{
    self.margin = (HH_SCREEN_WIDTH - 2*self.btnW)/3;
    self.loadDataBtn.frame = CGRectMake(self.margin, self.y, self.btnW, self.btnH);
    self.closeBtn.frame = CGRectMake(2*self.margin + self.btnW, self.y, self.btnW, self.btnH);
    
    self.showDataBtn.hidden = YES;
    self.closeBtn.hidden = NO;
}

- (void)loadEvent
{
    self.adManager = [[AdMoreDrawAdManager alloc] initWithSlotID:kDrawID rootController:kRootViewController];
    self.adManager.delegate = self;
    [self.view showActivityHUD];
    [self.adManager loadAdDataWithCount:1];
}

- (void)closeEvent
{
    [self.lastShowView removeFromSuperview];
}

#pragma mark ---------------------AdMoreDrawAdManagerDelegate----------------------------
/**draw广告加载成功*/
- (void)drawAdViewsLoadSuccess:(NSArray <AdMoreDrawAd *>*)drawAds
{
    if(drawAds.count > 0) //只加载1个的情况
    {
        self.drawAd = drawAds.firstObject;
        self.drawAd.delegate = self;
    }
}
/**draw广告加载失败**/
- (void)drawAdViewsFailedToLoadWithError:(NSError *)error
{
    [self.view hideActivityHUD];
    NSLog(@"广告加载失败%@",error.description);
}


#pragma mark ---------------------AdMoreDrawAdDelegate----------------------------
/**广告渲染成功*/
- (void)drawAdViewRenderSuccess:(UIView *)drawAdView drawAd:(nonnull AdMoreDrawAd *)drawAd
{
    [self.lastShowView removeFromSuperview];
    [self.contentView addSubview:drawAdView];
    self.contentView.contentSize = CGSizeMake(HH_SCREEN_WIDTH, drawAdView.height);
    self.lastShowView  = drawAdView;
    NSLog(@"draw:广告渲染成功");
    [self.view hideActivityHUD];
}
/**广告渲染失败*/
- (void)drawAdViewFailedToRender:(UIView *)drawAdView drawAd:(nonnull AdMoreDrawAd *)drawAd error:(nonnull NSError *)error
{
    [self.view hideActivityHUD];
    NSLog(@"draw:广告渲染失败%@",error.description);
}

- (void)drawAdViewWillExpose:(UIView *)drawAdView drawAd:(AdMoreDrawAd *)drawAd
{
    NSLog(@"draw:展示成功");
}

/**广告点击事件**/
- (void)drawAdViewDidClick:(UIView *)drawAdView drawAd:(nonnull AdMoreDrawAd *)drawAd
{
    NSLog(@"draw:广告点击");
}

/**视频完成播放**/
- (void)drawAdViewPlayerStopPlaying:(UIView *)drawAdView drawAd:(nonnull AdMoreDrawAd *)drawAd
{
    NSLog(@"draw:视频完成播放");

}
/**视频开始播放**/
- (void)drawAdViewVideoStartPlaying:(UIView *)drawAdView drawAd:(nonnull AdMoreDrawAd *)drawAd
{
    NSLog(@"draw:广告正在播放");
}
/**视频暂停播放**/
- (void)drawAdViewVideoDidPause:(UIView *)drawAdView drawAd:(nonnull AdMoreDrawAd *)drawAd
{
    NSLog(@"draw:视频暂停播放");
}

#pragma mark ---------------------getter----------------------------
- (UIScrollView *)contentView
{
    if(!_contentView)
    {
        _contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.loadDataBtn.bottom + 10, HH_SCREEN_WIDTH, HH_SCREEN_HEIGHT - self.loadDataBtn.bottom - 10)];
    }
    
    return _contentView;
}
@end
